<!--##########################################
    ##### Autor: Yngrid Coello           #####
    ##### Correo: y-c-@hotmail.com       #####
    ##### Ult. Modif: 26/10/2011         #####
    ##### Pasantias de Grado - USB       #####
    ##### Realización del Sistema: 0Bs   #####
    ##########################################-->
<?php

class empresaActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    // Se define la cantidad de resultados que se quieren por pagina
    $pager = new sfPropelPager('Compania', sfConfig::get('app_max_companias'));
    $pager->setCriteria(new Criteria());
    // Se establece que la pagina principal del index sera la 1
    $pager->setPage($this->getRequestParameter('page', 1));
    $pager->init();
     // Se Prepara la informaci�n para usarla en la vista.
    $this->pager = $pager;
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->Compania = CompaniaPeer::retrieveByPk($request->getParameter('id'));
    $this->forward404Unless($this->Compania);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new CompaniaForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new CompaniaForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($Compania = CompaniaPeer::retrieveByPk($request->getParameter('id')), sprintf('Object Compania does not exist (%s).', $request->getParameter('id')));
    $this->form = new CompaniaForm($Compania);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($Compania = CompaniaPeer::retrieveByPk($request->getParameter('id')), sprintf('Object Compania does not exist (%s).', $request->getParameter('id')));
    $this->form = new CompaniaForm($Compania);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($Compania = CompaniaPeer::retrieveByPk($request->getParameter('id')), sprintf('Object Compania does not exist (%s).', $request->getParameter('id')));
    $rif = CompaniaPeer::doSelectCompaniaById($request->getParameter('id'));
    $rif = $rif->getRif();
    $Compania->delete();
    $numero = $rif;
    LogPeer::logBorrar($numero,$this->getUser(),'Compania');
    $this->redirect('empresa/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {

    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $Compania = $form->save();
      $numero = $request->getPostParameter('compania[rif]');
      LogPeer::log($numero,$this->getUser(),'Compania');
      $this->redirect('empresa/index');
    }
  }
}
